#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# Copyright (C) 2021-2023 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a couple of tests with bconsole and console ACL
#
TestName="console-acl-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

echo "$cwd/build/po" > $tmp/file-list

export JobName HOST tmp conf
change_jobname NightlySave $JobName

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Autoprune", "no", "Director")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "File Retention", "2s", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "File Retention", "2s", "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "FileSet", "empty", "Job", "RestoreFiles")'

$rscripts/setup_rconsole

start_test

cat <<EOF > $tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label volume=TestVolume001 pool=Default storage=File
label volume=TestVolume002 pool=Test storage=File
run job=Simple level=full pool=Test client=$HOST-fd yes
run job=$JobName level=full client=$CLIENT yes
wait
messages
quit
EOF

# start the director
run_bacula

mv $conf/bconsole.conf $conf/bconsole.conf.old
mv $tmp/bconsole.conf2  $conf/bconsole.conf

cat <<EOF > $tmp/bconcmds
@##############################################
@$out $tmp/test1-fd.out
run job=$JobName level=full client=test1-fd yes
@sleep 2
status client=test1-fd
wait
messages
run job=$JobName level=full client=test1-fd yes
wait
messages
restore select all done yes where=$tmp/test1-fd-restore client=test1-fd
wait
messages
@$out $tmp/test-bkp-fd.out
@##############################################
run job=$JobName level=full client=test-bkp-fd yes
@sleep 2
status client=test-bkp-fd
wait
messages
run job=$JobName level=full client=test-bkp-fd yes
wait
messages
list jobs client=test-bkp-fd
restore select all done yes where=$tmp/test-bkp-fd-restore client=test-bkp-fd
wait
@$out $tmp/test-rst-fd.out
@##############################################
run job=$JobName level=full client=test-rst-fd yes
wait
restore select all done yes client=test-bkp-fd restoreclient=test-rst-fd where=$tmp/test-rst-fd-restore-1
wait
restore select all done yes client=test1-fd restoreclient=test-rst-fd where=$tmp/test-rst-fd-restore-2
wait
messages
@$out $tmp/whereacl.out
@##############################################
restore select all done yes client=test-bkp-fd restoreclient=test-rst-fd where=$tmp
wait
restore select all done yes client=test1-fd restoreclient=test-rst-fd add_suffix=.old
wait
restore select all done client=test-bkp-fd restoreclient=test-rst-fd
mod
9
/tmp
yes
wait
restore select all done client=test-bkp-fd restoreclient=test-rst-fd
mod
10
2
.old
6
yes
messages
quit
EOF
run_bconsole

nb=`grep "specification not authorized" $tmp/whereacl.out | wc -l`
if [ $nb != 4 ]; then
    print_debug "ERROR: Should find 4 Where not authorized in $tmp/whereacl.out"
    estat=2
fi

nb=`grep "Backup OK" $tmp/test1-fd.out | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: Should find 2 backups in $tmp/test1-fd.out"
    estat=1
fi

nb=`grep "Restore OK" $tmp/test1-fd.out | wc -l`
if [ $nb != 1 ]; then
    print_debug "ERROR: Should find 1 restore in $tmp/test1-fd.out"
    estat=1
fi

nb=`grep "Backup OK" $tmp/test-bkp-fd.out | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: Should find 2 backups in $tmp/test-bkp-fd.out"
    estat=1
fi

nb=`grep "Restore OK" $tmp/test-bkp-fd.out | wc -l`
if [ $nb != 0 ]; then
    print_debug "ERROR: Should not find a restore in $tmp/test-bkp-fd.out"
    estat=1
fi

nb=`grep "Backup OK" $tmp/test-rst-fd.out | wc -l`
if [ $nb != 0 ]; then
    print_debug "ERROR: Should not find backups in $tmp/test-rst-fd.out"
    estat=1
fi

nb=`grep "Restore OK" $tmp/test-rst-fd.out | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: Should find two restores in $tmp/test-rst-fd.out"
    estat=1
fi


# We need to test almost all kind of queries
for i in job client clients pool fileset
do
    cat <<EOF > $tmp/bconcmds
@$out $tmp/list-$i-job.out
list jobs
@$out $tmp/llist-$i-job.out
llist jobs
@$out $tmp/list-$i-joblog.out
list joblog jobid=1
list joblog jobid=2
list joblog jobid=3
@$out $tmp/llist-$i-joblog.out
llist joblog jobid=1
llist joblog jobid=2
llist joblog jobid=3
@$out $tmp/list-$i-client.out
list clients
@$out $tmp/llist-$i-client.out
llist clients
@$out $tmp/list-$i-pools.out
list pools
@$out $tmp/llist-$i-pools.out
llist pools
@$out $tmp/list-$i-pool.out
list pool=Default
@$out $tmp/llist-$i-pool.out
llist pool=Default
@$out $tmp/list-$i-media.out
list media
@$out $tmp/llist-$i-media.out
llist media
@$out $tmp/list-$i-volume.out
list volume=TestVolume001
@$out $tmp/llist-$i-volume.out
llist volume=TestVolume001
@$out $tmp/list-$i-jobmedia.out
list jobmedia
@$out $tmp/llist-$i-jobmedia.out
llist jobmedia
@$out $tmp/list-$i-jobmedia1.out
list jobmedia jobid=1
@$out $tmp/llist-$i-jobmedia1.out
llist jobmedia jobid=1
@$out $tmp/list-$i-files.out
list files jobid=1
@$out $tmp/llist-$i-files.out
llist files jobid=1
@$out $tmp/bvfs-$i-get_jobids.out
.bvfs_get_jobids jobid=3
messages
quit
EOF
    $bin/bconsole -c $tmp/bconsole.conf.$i < $tmp/bconcmds
done

################################################################
# We write here what we expect from the various tests.
#
# For each kind of ACL, we have the output of some commands
# in $tmp/list-<type>-*.out
#
# We will do some checks with AWK to count records, and we
# will compare with what we are supposed to found.
#
# We do tests with all kind of arguments to test a large variety
# of SQL queries
################################################################
i=job
mkdir $tmp/$i-target
# all clients are defined
echo 5 > $tmp/$i-target/nb-list-client.out
echo 5 > $tmp/$i-target/nb-llist-client.out

# for job restriction, we expect to see two jobs with client $HOST-fd
echo 5 > $tmp/$i-target/nb-list-job.out
echo 5 > $tmp/$i-target/nb-llist-job.out
echo 2 > $tmp/$i-target/nb-list-joblog.out
echo 2 > $tmp/$i-target/nb-llist-joblog.out
echo 5 > $tmp/$i-target/nb-list-jobmedia.out
echo 5 > $tmp/$i-target/nb-llist-jobmedia.out
echo   > $tmp/$i-target/nb-list-jobmedia1.out
echo   > $tmp/$i-target/nb-llist-jobmedia1.out
echo   > $tmp/$i-target/nb-list-files.out
echo   > $tmp/$i-target/nb-llist-files.out

# Job 3 is accessible
echo 1 > $tmp/$i-target/nb-bvfs-get_jobids.out

# all pools are defined
echo 2 > $tmp/$i-target/nb-list-media.out
echo 2 > $tmp/$i-target/nb-llist-media.out
echo 1 > $tmp/$i-target/nb-list-pool.out
echo 1 > $tmp/$i-target/nb-llist-pool.out
echo 2 > $tmp/$i-target/nb-list-pools.out
echo 2 > $tmp/$i-target/nb-llist-pools.out

################################################################
i=client
mkdir $tmp/$i-target
# all clients are defined
echo 1 > $tmp/$i-target/nb-list-client.out
echo 1 > $tmp/$i-target/nb-llist-client.out

echo 2 > $tmp/$i-target/nb-list-job.out
echo 2 > $tmp/$i-target/nb-llist-job.out
echo 2 > $tmp/$i-target/nb-list-joblog.out
echo 2 > $tmp/$i-target/nb-llist-joblog.out
echo 2 > $tmp/$i-target/nb-list-jobmedia.out
echo 2 > $tmp/$i-target/nb-llist-jobmedia.out
echo 1 > $tmp/$i-target/nb-list-jobmedia1.out
echo 1 > $tmp/$i-target/nb-llist-jobmedia1.out
echo 1 > $tmp/$i-target/nb-list-files.out
echo 1 > $tmp/$i-target/nb-llist-files.out

# Job 3 is not accessible
echo   > $tmp/$i-target/nb-bvfs-get_jobids.out

# all pools are defined
echo 2 > $tmp/$i-target/nb-list-media.out
echo 2 > $tmp/$i-target/nb-llist-media.out
echo 1 > $tmp/$i-target/nb-list-pool.out
echo 1 > $tmp/$i-target/nb-llist-pool.out
echo 2 > $tmp/$i-target/nb-list-pools.out
echo 2 > $tmp/$i-target/nb-llist-pools.out

################################################################
i=pool
mkdir $tmp/$i-target
# all clients are defined
echo 5 > $tmp/$i-target/nb-list-client.out
echo 5 > $tmp/$i-target/nb-llist-client.out

echo 9 > $tmp/$i-target/nb-list-job.out
echo 9 > $tmp/$i-target/nb-llist-job.out
echo 3 > $tmp/$i-target/nb-list-joblog.out
echo 3 > $tmp/$i-target/nb-llist-joblog.out
echo 6 > $tmp/$i-target/nb-list-jobmedia.out
echo 6 > $tmp/$i-target/nb-llist-jobmedia.out
echo 1 > $tmp/$i-target/nb-list-jobmedia1.out
echo 1 > $tmp/$i-target/nb-llist-jobmedia1.out
echo 1 > $tmp/$i-target/nb-list-files.out
echo 1 > $tmp/$i-target/nb-llist-files.out

# Job 3 is not accessible
echo   > $tmp/$i-target/nb-bvfs-get_jobids.out

echo 1 > $tmp/$i-target/nb-list-media.out
echo 1 > $tmp/$i-target/nb-llist-media.out
echo   > $tmp/$i-target/nb-list-pool.out
echo   > $tmp/$i-target/nb-llist-pool.out
echo 1 > $tmp/$i-target/nb-list-pools.out
echo 1 > $tmp/$i-target/nb-llist-pools.out

################################################################
i=clients
mkdir $tmp/$i-target
echo 2 > $tmp/$i-target/nb-list-client.out
echo 2 > $tmp/$i-target/nb-llist-client.out

echo 5 > $tmp/$i-target/nb-list-job.out
echo 5 > $tmp/$i-target/nb-llist-job.out
echo 3 > $tmp/$i-target/nb-list-joblog.out
echo 3 > $tmp/$i-target/nb-llist-joblog.out
echo 4 > $tmp/$i-target/nb-list-jobmedia.out
echo 4 > $tmp/$i-target/nb-llist-jobmedia.out
echo 1 > $tmp/$i-target/nb-list-jobmedia1.out
echo 1 > $tmp/$i-target/nb-llist-jobmedia1.out
echo 1 > $tmp/$i-target/nb-list-files.out
echo 1 > $tmp/$i-target/nb-llist-files.out

# Job 3 is accessible
echo 1 > $tmp/$i-target/nb-bvfs-get_jobids.out

echo 2 > $tmp/$i-target/nb-list-media.out
echo 2 > $tmp/$i-target/nb-llist-media.out
echo 1 > $tmp/$i-target/nb-list-pool.out
echo 1 > $tmp/$i-target/nb-llist-pool.out
echo 2 > $tmp/$i-target/nb-list-pools.out
echo 2 > $tmp/$i-target/nb-llist-pools.out

################################################################
i=fileset
mkdir $tmp/$i-target
# all clients are defined
echo 5 > $tmp/$i-target/nb-list-client.out
echo 5 > $tmp/$i-target/nb-llist-client.out

echo 8 > $tmp/$i-target/nb-list-job.out
echo 8 > $tmp/$i-target/nb-llist-job.out
echo 2 > $tmp/$i-target/nb-list-joblog.out
echo 2 > $tmp/$i-target/nb-llist-joblog.out
echo 5 > $tmp/$i-target/nb-list-jobmedia.out
echo 5 > $tmp/$i-target/nb-llist-jobmedia.out
echo   > $tmp/$i-target/nb-list-jobmedia1.out
echo   > $tmp/$i-target/nb-llist-jobmedia1.out
echo   > $tmp/$i-target/nb-list-files.out
echo   > $tmp/$i-target/nb-llist-files.out

# Job 3 is accessible
echo 1 > $tmp/$i-target/nb-bvfs-get_jobids.out

# all pools are defined
echo 2 > $tmp/$i-target/nb-list-media.out
echo 2 > $tmp/$i-target/nb-llist-media.out
echo 1 > $tmp/$i-target/nb-list-pool.out
echo 1 > $tmp/$i-target/nb-llist-pool.out
echo 2 > $tmp/$i-target/nb-list-pools.out
echo 2 > $tmp/$i-target/nb-llist-pools.out

# Here we generate some basic metrics for all tests,
# we mostly count lines in various files and we compare
# what we found with what we expect
for i in job client pool clients fileset
do
    mkdir -p $tmp/$i

    awk '/^\| +[0-9]/ {total++} END {print total}' $tmp/list-$i-client.out > $tmp/$i/nb-list-client.out
    awk 'tolower($1) ~ /clientid/ {total++} END {print total}' $tmp/llist-$i-client.out > $tmp/$i/nb-llist-client.out

    awk '/^\| +[0-9]/ {total++} END {print total}' $tmp/list-$i-job.out > $tmp/$i/nb-list-job.out
    awk 'tolower($1) ~ /^jobid/ {total++} END {print total}' $tmp/llist-$i-job.out > $tmp/$i/nb-llist-job.out

    awk '/Start Backup/ {total++} END {print total}' $tmp/list-$i-joblog.out > $tmp/$i/nb-list-joblog.out
    awk '/Start Backup/ {total++} END {print total}' $tmp/llist-$i-joblog.out > $tmp/$i/nb-llist-joblog.out

    # The list jobmedia jobid=x return also the job records, so we need to count +1
    awk '/^\| +[0-9]/ {total++} END {print total}' $tmp/list-$i-jobmedia.out > $tmp/$i/nb-list-jobmedia.out
    awk 'tolower($1) ~ /^jobid/ {total++} END {print total}' $tmp/llist-$i-jobmedia.out > $tmp/$i/nb-llist-jobmedia.out

    awk '/^\| +[0-9]/ {total++} END {print total}' $tmp/list-$i-jobmedia1.out > $tmp/$i/nb-list-jobmedia1.out
    awk 'tolower($1) ~ /^jobid/ {total++} END {print total}' $tmp/llist-$i-jobmedia1.out > $tmp/$i/nb-llist-jobmedia1.out

    awk '/^\| +[0-9]/ {total++} END {print total}' $tmp/list-$i-media.out > $tmp/$i/nb-list-media.out
    awk 'tolower($1) ~ /mediaid/ {total++} END {print total}' $tmp/llist-$i-media.out > $tmp/$i/nb-llist-media.out

    awk '/^\| +[0-9]/ {total++} END {print total}' $tmp/list-$i-pool.out > $tmp/$i/nb-list-pool.out
    awk 'tolower($1) ~ /^poolid:/ {total++} END {print total}' $tmp/llist-$i-pool.out > $tmp/$i/nb-llist-pool.out

    awk '/^\| +[0-9]/ {total++} END {print total}' $tmp/list-$i-pools.out > $tmp/$i/nb-list-pools.out
    awk 'tolower($1) ~ /^poolid:/ {total++} END {print total}' $tmp/llist-$i-pools.out > $tmp/$i/nb-llist-pools.out

    awk '/fr.po/ {total++} END {print total}' $tmp/list-$i-files.out > $tmp/$i/nb-list-files.out
    awk '/fr.po/ {total++} END {print total}' $tmp/llist-$i-files.out > $tmp/$i/nb-llist-files.out

    awk '/^3$/ {total++} END {print total}' $tmp/bvfs-$i-get_jobids.out > $tmp/$i/nb-bvfs-get_jobids.out
    
    diff -Naur $tmp/$i-target $tmp/$i
    if [ $? -ne 0 ]; then
        print_debug "ERROR: Found differences between $tmp/$i-target and $tmp/$i"
        estat=1
    fi
done


cat <<EOF > $tmp/bconcmds
@##############################################
@$out $tmp/bvfs1-1.out
list jobs client=test1-fd
.bvfs_get_jobs client=test1-fd
messages
@$out $tmp/bvfs1-2.out
list jobs
.bvfs_get_jobs client=test-bkp-fd
messages
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.bvfs1 < $tmp/bconcmds

sed -i 's/bvfs1/bvfs2/' $tmp/bconcmds
$bin/bconsole -c $tmp/bconsole.conf.bvfs2 < $tmp/bconcmds

nb=`awk '/^[0-9] [0-9]+/ { total++ } END { print total }' $tmp/bvfs1-1.out`
if [ "$nb" != 2 ]; then
    print_debug "ERROR: Should find two jobs in $tmp/bvfs1-1.out"
    estat=1
fi

nb=`awk 'BEGIN { total=0 } /^[0-9] [0-9]+/ { total++ } END { print total }' $tmp/bvfs1-2.out`
if [ "$nb" != 0 ]; then
    print_debug "ERROR: Should find 0 job in $tmp/bvfs1-2.out"
    estat=1
fi

nb=`awk 'BEGIN { total=0 } /^[0-9] [0-9]+/ { total++ } END { print total }' $tmp/bvfs2-1.out`
if [ "$nb" != 0 ]; then
    print_debug "ERROR: Should find 0 job in $tmp/bvfs2-1.out"
    estat=1
fi

nb=`awk 'BEGIN { total=0 } /^[0-9] [0-9]+/ { total++ } END { print total }' $tmp/bvfs2-2.out`
if [ "$nb" != 0 ]; then
    print_debug "ERROR: Should find 0 job in $tmp/bvfs2-2.out"
    estat=1
fi

grep -i "error" $tmp/bvfs*out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Should not find errors in $tmp/bvfs*.out"
    estat=1
fi

################################################################
# Test restore command with different ACL

fn=$tmp/restore1-org.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
1
.
quit
EOF

$bin/bconsole -c $bin/bconsole.conf.old < $tmp/bconcmds

if ! grep Simple $fn > /dev/null
then
    print_debug "ERROR: Should find Simple job in $fn"
    estat=1
fi

if ! grep test-bkp-fd $fn > /dev/null
then
    print_debug "ERROR: Should find test-bkp-fd job in $fn"
    estat=1
fi

if ! grep test1-fd $fn > /dev/null
then
    print_debug "ERROR: Should find test1-fd job in $fn"
    estat=1
fi

fn=$tmp/restore1.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
1
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

if grep Simple $fn > /dev/null
then
    print_debug "ERROR: Should not find Simple job in $fn"
    estat=1
fi

if ! grep test-bkp-fd $fn > /dev/null
then
    print_debug "ERROR: Should find test-bkp-fd job in $fn"
    estat=1
fi

if ! grep test1-fd $fn > /dev/null
then
    print_debug "ERROR: Should find test1-fd job in $fn"
    estat=1
fi

fn=$tmp/restore2.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
1
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.client < $tmp/bconcmds

if ! grep Simple $fn > /dev/null
then
    print_debug "ERROR: Should find Simple job in $fn"
    estat=1
fi

if ! grep $HOST-fd $fn > /dev/null
then
    print_debug "ERROR: Should find $HOST-fd job in $fn"
    estat=1
fi

if grep test1-fd $fn > /dev/null
then
    print_debug "ERROR: Should not find test1-fd job in $fn"
    estat=1
fi

################################################################
# Test restore command with different ACL

fn=$tmp/restore3-org.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
2
1
uk.po
.
quit
EOF

$bin/bconsole -c $bin/bconsole.conf.old < $tmp/bconcmds

nb=`grep po/uk.po $fn | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: Should find uk.po 2 time in $fn, not $nb"
    estat=1
fi

if ! grep $HOST-fd $fn > /dev/null
then
    print_debug "ERROR: Should find $HOST-fd job in $fn"
    estat=1
fi

# Here we have multiple clients, and we have one jobs visible for
# the file on the default client
fn=$tmp/restore3.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
2
1
uk.po
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

nb=`grep po/uk.po $fn | wc -l`
if [ $nb != 1 ]; then
    print_debug "ERROR: Should find uk.po 1 time in $fn, not $nb"
    estat=1
fi

if ! grep $HOST-fd $fn > /dev/null
then
    print_debug "ERROR: Should find $HOST-fd job in $fn"
    estat=1
fi

################################################################
# Here we test item 3 with custom jobid list for restore

fn=$tmp/restore4-org.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
3
1,2,3,4,6,7
done
.
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

if ! grep "JobIds: 1,2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should not find full job list in $fn"
    estat=1
fi

fn=$tmp/restore4.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
3
1,2,3,4,6,7
done
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

if grep "JobIds: 1,2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should not find full job list in $fn"
    estat=1
fi

if ! grep "JobIds: 2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should not find full job list in $fn"
    estat=1
fi

# Here the client is automatically selected, and we have other jobs
# the file
fn=$tmp/restore5.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
3
1,2,3,4,6,7
done
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.client < $tmp/bconcmds

if grep "JobIds: 1,2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should not find full job list in $fn"
    estat=1
fi

if ! grep -E 'JobIds: 1,2$' $fn > /dev/null; then
    print_debug "ERROR: Should  find job list in $fn"
    estat=1
fi


##

################################################################
# Here we test command line option jobid= with custom jobid list for restore
fn=$tmp/restore6-org.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore jobid=1,2,3,4,6,7
done
.
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

if ! grep "JobIds: 1,2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should find full job list in $fn"
    estat=1
fi

################################################################
# Here we test command line option jobid= with custom jobid list for restore
fn=$tmp/restore6.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore jobid=1,2,3,4,6,7
done
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

if grep "JobIds: 1,2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should not find full job list in $fn"
    estat=1
fi

if ! grep -E 'JobIds: 2,3,4,6,7' $fn > /dev/null; then
    print_debug "ERROR: Should not find job list in $fn"
    estat=1
fi

# Here the client is automatically selected, and we have other jobs
# the file
fn=$tmp/restore7.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore jobid=1,2,3,4,6,7
done
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.client < $tmp/bconcmds

if grep "JobIds: 1,2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should not find full job list in $fn"
    estat=1
fi

if ! grep -E 'JobIds: 1,2$' $fn > /dev/null; then
    print_debug "ERROR: Should  find an error in $fn"
    estat=1
fi

##

################################################################
# Here we test restore command option 11
fn=$tmp/restore8.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
1
.
restore
11
.
restore
11
test1
restore
11
1,2,3,4
1
test1
/etc/
</etc/passwd
$cwd/build/po/

quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

if ! grep "No JobId selected" $fn > /dev/null; then
    print_debug "ERROR: Should find selection list error in $fn"
    estat=1
fi

if ! grep "Job not run" $fn > /dev/null; then
    print_debug "ERROR: Should find that job did not run in $fn"
    estat=1
fi

if ! grep "Operation not allowed" $fn > /dev/null; then
    print_debug "ERROR: Should find a message about < input in $fn"
    estat=1
fi

if ! grep "Bootstrap records written" $fn > /dev/null; then
    print_debug "ERROR: Should find that the restore wrote a bootstrap in $fn"
    estat=1
fi

# Here the client is automatically selected, and we have other jobs
# the file
fn=$tmp/restore9.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore
11
1,2,3,4,6,7
$cwd/build/po/

.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.client < $tmp/bconcmds

if grep "JobIds: 1,2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should not find full job list in $fn"
    estat=1
fi

if ! grep -E 'JobIds: 1,2$' $fn > /dev/null; then
    print_debug "ERROR: Should  find an error in $fn"
    estat=1
fi

################################################################
# Here we test restore command option 9
fn=$tmp/restore90.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobs
restore client=test-bkp-fd
9
.
restore client=$HOST-fd
9
.
restore
9
1
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

if grep "No FileSet found" $fn > /dev/null; then
    print_debug "ERROR: Should find selection list error in $fn"
    estat=1
fi

# We have the list and two menu
nb=`grep " 2 " $fn | wc -l`
if [ $nb != 3 ]; then
    print_debug "ERROR: Should find selection for jobid 2 ($nb) list in $fn"
    estat=1
fi

# We have the list and the last full
nb=`grep " 7 " $fn | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: Should find selection for jobid 7 ($nb) list in $fn"
    estat=1
fi

# Here the client is automatically selected, and we have other jobs
# the file
fn=$tmp/restore91.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
@exec "touch $cwd/build/po/fr.po"
run job=Simple level=differential yes
wait
@exec "touch $cwd/build/po/fr.po"
run job=Simple level=incremental yes
wait
@exec "touch $cwd/build/po/fr.po"
run job=Simple level=incremental yes
wait
messages
restore
9
2
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.client < $tmp/bconcmds

if ! grep "SimpleSet" $fn > /dev/null; then
    print_debug "ERROR: Should job list for SimpleSet in $fn"
    estat=1
fi

# We should find Full, Diff, Incr, Incr
if ! grep  ' 1 ' $fn > /dev/null; then
    print_debug "ERROR: Should find jobid 11 in $fn"
    estat=1
fi

if ! grep  ' 10 ' $fn > /dev/null; then
    print_debug "ERROR: Should find jobid 13 in $fn"
    estat=1
fi

if ! grep  ' 11 ' $fn > /dev/null; then
    print_debug "ERROR: Should find jobid 14 in $fn"
    estat=1
fi

if ! grep  ' 12 ' $fn > /dev/null; then
    print_debug "ERROR: Should find jobid 15 in $fn"
    estat=1
fi

################################################################
# Here we test restore command option 8
fn=$tmp/restore80.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobs
restore client=test-bkp-fd
8
`date "+%Y-%m-%d %H:%M:%S"`
1
$build/po/

no
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

if ! grep "No files selected to be restored." $fn > /dev/null; then
    print_debug "ERROR: Should not find restore list in $fn"
    estat=1
fi

fn=$tmp/restore81.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobs
restore client=test-bkp-fd
8
`date "+%Y-%m-%d %H:%M:%S"`
1
$build/po/

no
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

if ! grep "selected to be restored" $fn > /dev/null; then
    print_debug "ERROR: Should find restore list in $fn"
    estat=1
fi

################################################################
# Here we test restore command option 12
fn=$tmp/restore120.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobs
restore
12
11
m *
done
no
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.client < $tmp/bconcmds

if ! grep "files selected to be restored" $fn > /dev/null; then
    print_debug "ERROR: Should find restore list in $fn"
    estat=1
fi

fn=$tmp/restore121.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobs
restore
12
11
m *
done
no
quit
EOF

$bin/bconsole -c $bin/bconsole.conf.old < $tmp/bconcmds

if ! grep "selected to be restored" $fn > /dev/null; then
    print_debug "ERROR: Should find restore list in $fn"
    estat=1
fi

fn=$tmp/restore122.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobs
restore
12
11
quit
EOF

$bin/bconsole -c $bin/bconsole.conf < $tmp/bconcmds

if ! grep "Unable to get Job record" $fn > /dev/null; then
    print_debug "ERROR: Should not find restore list in $fn"
    estat=1
fi

################################################################
# Test the jobid from the command line
fn=$tmp/restore10-org.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore jobid=1 jobid=2,3 jobid=4,6,7
done
.
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

if ! grep "JobIds: 1,2,3,4,6,7" $fn > /dev/null; then
    print_debug "ERROR: Should find full job list in $fn"
    estat=1
fi

fn=$tmp/restore10.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore jobid=1 jobid=2,3 jobid=4,6,7
done
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.client < $tmp/bconcmds

if ! grep -E "JobIds: 1,2$" $fn > /dev/null; then
    print_debug "ERROR: Should find small job list in $fn"
    estat=1
fi

fn=$tmp/restore11.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore jobid=1 jobid=2,3 jobid=4,6,7
done
.
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

if ! grep -E "JobIds: 2,3,4,6,7$" $fn > /dev/null; then
    print_debug "ERROR: Should find small job list in $fn"
    estat=1
fi

##

################################################################
# Test the jobid from the command line
fn=$tmp/restore12-org.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
sql
CREATE TABLE f1 AS (SELECT JobId, FileIndex FROM File);

restore file=?f1 client=$HOST-fd
no
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

nb_max=`awk '/[0-9]+ files selected to be restored./ { print $1}' $fn`

if [ "$nb_max" = "" ]; then
    print_debug "ERROR: Unable to start restore with full file table $fn"
    estat=1
fi

# Test the jobid from the command line
fn=$tmp/restore12-err1.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore file="?(SELECT 1, 1)" client=$HOST-fd
no
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

nb=`awk '/[0-9]+ files selected to be restored./ { print $1}' $fn`

if [ "$nb" != "" ]; then
    print_debug "ERROR: Should not be able to start restore with bad table $fn"
    estat=1
fi

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

# Test the file argument from the command line
fn=$tmp/restore12-err2.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore file="?file" client=$HOST-fd
no
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

nb=`awk '/[0-9]+ files selected to be restored./ { print $1}' $fn`

if [ "$nb" != "" ]; then
    print_debug "ERROR: Should not be able to start restore with bad table $nb $fn"
    estat=1
fi

fn=$tmp/restore12.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore file=?f1 client=$HOST-fd
no
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.client < $tmp/bconcmds

nb=`awk '/files selected to be restored./ { print $1}' $fn`

if [ $nb = $nb_max ]; then
    print_debug "ERROR: Should not be able to start restore with full table $fn"
    estat=1
fi

fn=$tmp/restore13.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
restore file=?f1 client=$HOST-fd
no
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

nb=`awk '/files selected to be restored./ { print $1}' $fn`

if [ $nb = $nb_max ]; then
    print_debug "ERROR: Should not be able to start restore with full table $fn"
    estat=1
fi

cat <<EOF > $tmp/bconcmds
@##############################################
sql
DROP TABLE f1;

quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

################################################################
# Check prune, purge, delete commands
fn=$tmp/cmd1.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-before
list jobs
@output $fn-before
list files jobid=1
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobid=1
purge files jobid=1 yes
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

fn=$tmp/cmd1.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-after
list jobid=1
@output $fn-after
list files jobid=1
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

# Check if the job was here
if ! grep Simple $fn-before > /dev/null; then
    print_debug "ERROR: Should find the job Simple in $fn-before"
    estat=1
fi

# Check if the files was here
if ! grep uk.po $fn-before > /dev/null; then
    print_debug "ERROR: Should find the files in $fn-before"
    estat=1
fi

# Check if the job is still here
if ! grep Simple $fn-after > /dev/null; then
    print_debug "ERROR: Should find the job Simple in $fn-after"
    estat=1
fi

# Check if the files are still here
if ! grep uk.po $fn-after > /dev/null; then
    print_debug "ERROR: Should find the files in $fn-after"
    estat=1
fi

################
fn=$tmp/cmd2.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobid=1
purge jobs jobid=1 yes
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-after
list jobid=1
@output $fn-after
list files jobid=1
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

# Check if the job is still here
if ! grep Simple $fn-after > /dev/null; then
    print_debug "ERROR: Should find the job Simple in $fn-after"
    estat=1
fi

# Check if the files are still here
if ! grep uk.po $fn-after > /dev/null; then
    print_debug "ERROR: Should find the files in $fn-after"
    estat=1
fi

################
fn=$tmp/cmd3.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobid=1
delete jobid=1 yes
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-after
list jobid=1
@output $fn-after
list files jobid=1
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

# Check if the job is still here
if ! grep Simple $fn-after > /dev/null; then
    print_debug "ERROR: Should find the job Simple in $fn-after"
    estat=1
fi

# Check if the files are still here
if ! grep uk.po $fn-after > /dev/null; then
    print_debug "ERROR: Should find the files in $fn-after"
    estat=1
fi

################
fn=$tmp/cmd4.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobid=1
prune volume=TestVolume001 yes
prune volume=TestVolume002 yes
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-after
list jobid=1
@output $fn-after
list files jobid=1
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

# Check if the job is still here
if ! grep Simple $fn-after > /dev/null; then
    print_debug "ERROR: Should find the job Simple in $fn-after"
    estat=1
fi

# Check if the files are still here
if ! grep uk.po $fn-after > /dev/null; then
    print_debug "ERROR: Should find the files in $fn-after"
    estat=1
fi

################
fn=$tmp/cmd5.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobid=1
purge volume=TestVolume001 yes
purge volume=TestVolume002 yes
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-after
list jobid=1
@output $fn-after
list files jobid=1
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

# Check if the job is still here
if ! grep Simple $fn-after > /dev/null; then
    print_debug "ERROR: Should find the job Simple in $fn-after"
    estat=1
fi

# Check if the files are still here
if ! grep uk.po $fn-after > /dev/null; then
    print_debug "ERROR: Should find the files in $fn-after"
    estat=1
fi

################
fn=$tmp/cmd6.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
run job=Slow yes
run job=Slow yes
.status dir running
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-after
cancel jobid=13 yes
cancel all yes
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

cat <<EOF > $tmp/bconcmds
@$out $fn
@##############################################
wait
messages
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

if grep "Confirm cancel" $fn-after > /dev/null; then
    print_debug "ERROR: Should not be able to see jobs in $fn-after"
    estat=1
fi

if ! grep "Unauthorized command from this console for JobId" $fn-after > /dev/null; then
    print_debug "ERROR: Should find Unauthorized message in $fn-after"
    estat=1
fi

nb=`grep "Backup OK" $fn | wc -l`

if [ $nb -ne 2 ]; then
    print_debug "ERROR: Should not find canceled jobs in $fn"
    estat=1
fi

if [ -f build/src/plugins/fd/postgresql-fd.c ]; then
    make -C build/src/plugins/fd/ install-postgresql
    $bin/bacula-ctl-fd restart
    cat <<EOF >> $conf/bacula-dir.conf
FileSet {
        Name = pgbackup
        Include   {
                  Plugin = "postgresql: use_sudo"
        }
}
EOF
    fn=$tmp/pg1.out
    cat <<EOF > $tmp/bconcmds
@$out $fn
reload
run job=Simple fileset=pgbackup yes
wait
message
list objects jobid=15
restore
13
1
1
1
no
quit
EOF
    $bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds    
    
    if ! grep postgres $fn > /dev/null; then
        print_debug "ERROR: Should find a postgres database in $fn"
        estat=1
    fi
    if ! grep "files selected to be restored" $fn > /dev/null; then
        print_debug "ERROR: Should find a restore started in $fn"
        estat=1
    fi

    fn=$tmp/pg2.out
    cat <<EOF > $tmp/bconcmds
@$out $fn
list objects jobid=15
restore
13
quit
EOF
    $bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds   

    if grep "PostgreSQL | postgres" $fn > /dev/null; then
        print_debug "ERROR: Should not find object in $fn"
        estat=1
    fi

    if ! grep "is empty" $fn > /dev/null; then
        print_debug "ERROR: Should not find object in $fn"
        estat=1
    fi
fi

################
fn=$tmp/delete1.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
update volume=TestVolume001 volstatus=Used
update volume=TestVolume002 volstatus=Used
label volume=TestVolume003 pool=Default storage=File slot=0 index=0 yes
run job=Simple level=full yes
wait
message
list jobmedia volume=TestVolume003
delete volume=TestVolume003 yes
@exec "rm -f $tmp/TestVolume003"
@$out $fn-delete
list volumes
list jobmedia volume=TestVolume003
label volume=TestVolume003 pool=Default storage=File slot=0 index=0 yes
@$out $fn-recreate
run job=Simple level=full yes
wait
message
list jobmedia volume=TestVolume003
list volumes
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

# Here everything should work

if grep "TestVolume003 | Append" $fn-delete > /dev/null; then
    print_debug "ERROR: Should not find TestVolume003 in $fn-delete"
    estat=1
fi

if ! grep "TestVolume003 | Append" $fn-recreate > /dev/null; then
    print_debug "ERROR: Should find TestVolume003 in $fn-recreate"
    estat=1
fi

if ! grep "16 | TestVolume003" $fn-recreate > /dev/null; then
    print_debug "ERROR: Should find Job 16 on TestVolume003 in $fn-recreate"
    estat=1
fi

fn=$tmp/delete2.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
list jobs
list volumes
list jobmedia volume=TestVolume003
delete volume=TestVolume003 yes
list volumes
list jobmedia volume=TestVolume003
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job < $tmp/bconcmds

# Here the command should not work and the volume
# and the jobs should still be present

if ! grep "TestVolume003 | Append" $fn > /dev/null; then
    print_debug "ERROR: Should find TestVolume003 in $fn-delete"
    estat=1
fi

if ! grep "TestVolume003 | Append" $fn > /dev/null; then
    print_debug "ERROR: Should find TestVolume003 in $fn"
    estat=1
fi

if grep "17 | TestVolume003" $fn > /dev/null; then
    print_debug "ERROR: Should not  find TestVolume003 jobmedia in $fn"
    estat=1
fi

################
fn=$tmp/purge-jobid.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
update volume=TestVolume001 volstatus=Append
update volume=TestVolume002 volstatus=Append
run job=Simple client=test1-fd level=full yes
wait
message
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

# Here everything should work

cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-purge
restart jobid=17
wait
message
list files jobid=18
purge files jobid=18 yes
list files jobid=18
list jobs
purge jobs name=Simple client=test1-fd yes
list jobs
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.job-client < $tmp/bconcmds

if grep " 18 " $fn-purge; then
    print_debug "ERROR: Should not even find jobid 18 in $fn-purge"
    estat=1
fi

if ! grep 'No authorization. Job "Simple"' $fn-purge > /dev/null; then
    print_debug "ERROR: Should find issue with restart in $fn-purge"
    estat=1
fi

nb=`grep uk.po $fn-purge | wc -l`
if [ $nb != 0 ]; then
    print_debug "ERROR: Should not find uk.po file in $fn-purge"
    estat=1
fi

# Try to purge jobid 18
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn-after-purge
restart jobid=17
wait
message
list files jobid=18
purge files jobid=18 yes
list files jobid=18

list jobs
purge jobs name=Simple client=test1-fd yes
list jobs
quit
EOF

$bin/bconsole -c $conf/bconsole.conf.old < $tmp/bconcmds

nb=`grep " 18 " $fn-after-purge | wc -l`
if [ $nb != 3 ]; then
    print_debug "ERROR: Should find 3 times jobid 18 in $fn-after-purge"
    estat=1
fi

nb=`grep uk.po $fn-after-purge | wc -l`
if [ $nb != 1 ]; then
    print_debug "ERROR: Should find 1 time the uk.po file in $fn-after-purge"
    estat=1
fi

if ! grep "Backup OK" $fn-after-purge > /dev/null; then
    print_debug "ERROR: Should find restart job $fn-after-purge"
    estat=1
fi

# Try to restore without the fileset configured
fn=$tmp/restore.out
cat <<EOF > $tmp/bconcmds
@##############################################
@$out $fn
.fileset
run job=backup level=full yes
wait
message
restore client=$HOST-fd
5
m *
done
yes
wait
message
quit
EOF

$bin/bconsole -c $tmp/bconsole.conf.fileset < $tmp/bconcmds

# It should select a different fileset by default
# as "empty" is not available for this console
if grep empty $fn; then
    print_debug "ERROR: Should not find the empty fileset in $fn"
    estat=1
fi

stop_bacula
end_test
